package com.young.fragments.demo.simple_jump_demo.fms

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.young.fragments.databinding.FmSimpleJumpTarget1Binding
import com.young.lib_fragments.fms.BaseLazyFragment

/**
 * @author huangzehui
 * @date 2025/1/12 22:14 周日
 * @description 简单跳转的目标fragment
 */
class SimpleJumpTargetFragment1 : BaseLazyFragment(), View.OnClickListener {

    private lateinit var mBinding: FmSimpleJumpTarget1Binding

    companion object {
        // 回退栈信息的名称
        val BACK_STACK_NAME = "third"
    }

    // 回退栈的 id
    private var mBackStackId = -1

    // 是否自定义回退
    private var mIsCustomBack = false

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        mBinding = FmSimpleJumpTarget1Binding.inflate(inflater, container, false)
        return mBinding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)


        val backStackEntryCount = parentFragmentManager.backStackEntryCount
        if (backStackEntryCount > 0) {
            mBinding.tvText.text = "fmTag=${BACK_STACK_NAME} \nid=${
                parentFragmentManager.getBackStackEntryAt(backStackEntryCount - 1).id
            }"
            mBackStackId = parentFragmentManager.getBackStackEntryAt(0).id
        } else {
            mBinding.tvText.text = "fmTag=${SimpleJumpTargetFragment.BACK_STACK_NAME} id=未知"
        }


        initClick()

//        for (i in 0 until backStackEntryCount){
//            val entry = parentFragmentManager.getBackStackEntryAt(i)
//            Log.e("kkk","entry = ${entry} = ${entry.name}")
//        }

        showIsCustomBackInfo()

    }

    override fun onBackPress() {
        super.onBackPress()
        Log.e("young_test9090909", "onBackPress")
//        FmTransitionsUtil.pop2TargetFmCls(
//            parentFragmentManager,
//            SimpleJumpDemoFragment::class.java,
//            false
//        )

        pop2TargetFmCls(
            SimpleJumpDemoFragment::class.java,
            false
        )
    }

    override fun onClick(v: View?) {
        when (v) {
            mBinding.btnCustomBack -> {
                mIsCustomBack = !mIsCustomBack
                setEnableBackPress(mIsCustomBack)
                showIsCustomBackInfo()
            }

            mBinding.btnLast -> {
//                FmTransitionsUtil.pop(parentFragmentManager)
                pop()
            }

            mBinding.btnLast1 -> {
//                FmTransitionsUtil.pop2Root(parentFragmentManager)
                pop2Root()
            }

            mBinding.btnLast2 -> {
//                FmTransitionsUtil.pop2BackStackNameFm(
//                    parentFragmentManager,
//                    SimpleJumpTargetFragment.BACK_STACK_NAME,
//                    true
//                )
                pop2BackStackNameFm(
                    SimpleJumpTargetFragment.BACK_STACK_NAME,
                    true
                )
            }

            mBinding.btnLast3 -> {
//                FmTransitionsUtil.pop2BackStackNameFm(
//                    parentFragmentManager,
//                    SimpleJumpTargetFragment.BACK_STACK_NAME,
//                    false
//                )
                pop2BackStackNameFm(
                    SimpleJumpTargetFragment.BACK_STACK_NAME,
                    false
                )
            }

            mBinding.btnLast4 -> {
//                FmTransitionsUtil.pop2IdFm(parentFragmentManager, mBackStackId, true)
                pop2IdFm(mBackStackId, true)
            }

            mBinding.btnLast5 -> {
//                FmTransitionsUtil.pop2IdFm(parentFragmentManager, mBackStackId, false)
                pop2IdFm(mBackStackId, false)
            }

            mBinding.btnLast6 -> {
//                FmTransitionsUtil.pop2TargetFmCls(
//                    parentFragmentManager,
//                    SimpleJumpDemoFragment::class.java,
//                    true
//                )
                pop2TargetFmCls(
                    SimpleJumpDemoFragment::class.java,
                    true
                )
            }

            mBinding.btnLast7 -> {
//                FmTransitionsUtil.pop2TargetFmCls(
//                    parentFragmentManager,
//                    SimpleJumpDemoFragment::class.java,
//                    false
//                )
                pop2TargetFmCls(
                    SimpleJumpDemoFragment::class.java,
                    false
                )
            }

            mBinding.btnLast8 -> {
//                FmTransitionsUtil.addWithPopFm(
//                    fmManager = parentFragmentManager,
//                    containerViewId = getFragmentContainerId(),
//                    fm = SimpleJumpTargetFragment2(),
//                    lastFm = this,
//                )
                startWithPopFm(
                    fm =SimpleJumpTargetFragment2(),
                    lastFm = this,
                )
            }

            else -> {
                throw Exception("没有点击处理")
            }
        }
    }


    private fun initClick() {
        mBinding.btnLast.setOnClickListener(this)
        mBinding.btnLast1.setOnClickListener(this)
        mBinding.btnLast2.setOnClickListener(this)
        mBinding.btnLast3.setOnClickListener(this)
        mBinding.btnLast4.setOnClickListener(this)
        mBinding.btnLast5.setOnClickListener(this)
        mBinding.btnLast6.setOnClickListener(this)
        mBinding.btnLast7.setOnClickListener(this)
        mBinding.btnLast8.setOnClickListener(this)

        mBinding.btnCustomBack.setOnClickListener(this)
    }

    /**
     * 展示是否拦截按钮的信息
     */
    private fun showIsCustomBackInfo() {
        mBinding.btnCustomBack.text = "是否拦截返回（拦截后，处理返回根Fragment）:${mIsCustomBack}"
    }

}